package com.luck.pf.common.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.org.entity.SysLog;
import com.luck.org.entity.SysMenu;
import com.luck.org.entity.SysUser;
import com.luck.pf.common.Constants;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.common.logic.CommonQry;
import com.luck.pf.common.logic.ICommonLogic;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.security.menu.logic.IMenuLogic;
import com.luck.pf.security.user.logic.IUserLogic;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;
import com.luck.pf.util.ToolUtil;
import com.opensymphony.xwork2.ActionContext;

public class CommonAction extends BaseActionSupport{
	private static final long serialVersionUID = -1980405701847417769L;
	ICommonLogic commonLogic;
	private IMenuLogic menuLogic;
	private IUserLogic userLogic;
	private CommonQry qry;
	private Page page;
	public String logs() throws Exception{
		if (qry == null) {
			qry = new CommonQry();
		}
		if(qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -1)));
		if(qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		if(StringUtils.isNotBlank(qry.getOperateType()))
			page = commonLogic.findLogsPage(qry);
		return "logs";
	}

	/**
	 * SpringSecurity登陆成功后跳转到主面
	 * @return
	 * @throws Exception
	 */
	public String bridge() throws Exception{
		HttpSession session = getRequest().getSession();
		String loginname = (String) session.getAttribute(Constants.SESSION_LOGIN_NAME);
		if (ToolUtil.isEmpty(loginname)){
			super.setMsgStr("用户没有登陆");
			return "login";
		}
		UserBean userBean = userLogic.findUserBean(loginname);
		String tips = commonLogic.checkPayment(userBean.getOrgId());
		List<SysMenu> menus = null;
		if(StringUtils.isBlank(tips))
			menus = menuLogic.findUserMenu(loginname,Constants.ROOT_FLAG,userBean.getUser().getSystemFlag());
		else
			ActionContext.getContext().getSession().put(Constants.SESSION_TIPS,tips);	
		
		ActionContext.getContext().getSession().put(Constants.SESSION_MENUS,menus);
		ActionContext.getContext().getSession().put(Constants.SESSION_USER_BEAN, userBean);
		//创建登陆日志
		SysLog log = super.createLog(userBean, "登陆成功", "1");
		commonLogic.save(log);
		//记录登陆 时间
		SysUser user = userLogic.findUserByLoginname(loginname);
		user.setLastLoginDate(DateUtil.getDate());
		userLogic.save(user);
		return "bridge";
	}
	
	/**
	 * 保存连接地址
	 * @return
	 * @throws Exception
	 */
	public String keepLive() throws Exception {
		super.rendText("keep-live");
		return null;
	}
	
	public String help() throws Exception{
		return "help";
	}

	public String toEditPw() throws Exception{
		return "editPw";
	}
	public String homepage() throws Exception{
		return "homepage";
	}
	public IMenuLogic getMenuLogic() {
		return menuLogic;
	}

	public void setMenuLogic(IMenuLogic menuLogic) {
		this.menuLogic = menuLogic;
	}

	public IUserLogic getUserLogic() {
		return userLogic;
	}

	public void setUserLogic(IUserLogic userLogic) {
		this.userLogic = userLogic;
	}
	public ICommonLogic getCommonLogic() {
		return commonLogic;
	}
	public void setCommonLogic(ICommonLogic commonLogic) {
		this.commonLogic = commonLogic;
	}
	public CommonQry getQry() {
		return qry;
	}
	public void setQry(CommonQry qry) {
		this.qry = qry;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}

}
